﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;

namespace Yuanlin.Business
{
    public class ZiCaiInfos : YuanlinUserControl,
        IReplyConverter<ZiCaiInfoReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        public int kind = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Kind);
        public int zicaiTopLevelCategoryID = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ZicaiTopLevelCategoryID);
        public int zicaiSecondLevelCategoryID = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ZicaiSecondLevelCategoryID);
        public string tradeType = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.TradeType);
        private EntityList ziCaiInfos;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack)
            {
                //从公司资材中得到所有第2层资材类别
                ZiCaiInfoListRequest request = new ZiCaiInfoListRequest();
                request.UserId = UserIdFromGreenNum;
                request.TradeType = tradeType;
                request.Kind = kind;
                if (zicaiTopLevelCategoryID > 0)
                {
                    request.TopCategory = zicaiTopLevelCategoryID;
                }
                request.SecondCategory = zicaiSecondLevelCategoryID;
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                requestBinders.Add(RequestHelper.BuildGetZiCaiInfoListRequest(this, request));
            }
        }

        private void BindData()
        {
            list.DataSource = ziCaiInfos;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(ziCaiInfos.TotalCount);
                currentPage.TotalRecords = (ziCaiInfos.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region IReplyConverter<ZiCaiInfoReply,GetListOperation> Members

        public void ConvertReply(ZiCaiInfoReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                ziCaiInfos = reply.EntityList;
                if (!IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}